import { Injectable } from '@angular/core';
@Injectable({
  providedIn: 'root'
})
export class HttpService {

  constructor() { }
  /*分页函数开始*/
  private index_sum = 5;     // 5个点击的指针
  private num_10 = 10;     // 排列10条数据
  private page_sum: any; // 总页数
  Pointer( num_sum , cb ) {
    this.page_sum = Math.ceil( num_sum / this.num_10); // 计算多小页
    let click_box = [] ;
    let index = 0 ;
    if ( this.page_sum < 5) {
      index = this.page_sum ;
    } else {
      index = 0 ;
    }
    switch (index) {
      case 0:
        click_box = [0, 1, 2, 3, 4];
        break;
      case 1:
        click_box = [0];
        break;
      case 2:
        click_box = [0, 1];
        break;
      case 3:
        click_box = [0, 1, 2];
        break;
      case 4:
        click_box = [0, 1, 2, 3];
        break;
      default:
    }
    cb(this.page_sum , click_box );
  }
  next(Pointer_index, dot_num , click_box , page_sum , cb ) {
    if ( Pointer_index % this.index_sum == 0 && Pointer_index < page_sum) {
      dot_num += 5;
    }
    if (Pointer_index >= (page_sum - page_sum % 5)) { // 判断最后一页 隐藏没的按钮
      let index = page_sum % 5;  // 数据后面删除index个
      if ( page_sum < 5) {
        index = page_sum ;
      }
      switch (index) {
        case 1:
          click_box = [0];
          break;
        case 2:
          click_box = [0, 1];
          break;
        case 3:
          click_box = [0, 1, 2];
          break;
        case 4:
          click_box = [0, 1, 2, 3];
          break;
        default:
      }
    }
    if (Pointer_index < page_sum) {
      Pointer_index++;
    } else {
      Pointer_index = page_sum;
    }
    cb(Pointer_index, dot_num , click_box);
  }
  pre(Pointer_index, dot_num, click_box , page_sum , cb) {
    if (Pointer_index > 1) {
      Pointer_index--;
    } else {
      Pointer_index = 1;
    }
    if (Pointer_index > (page_sum - page_sum % 5)) { // 判断最后一页
      const index = page_sum % 5 ;
      click_box.splice(index , 5);
    } else {
      click_box = [0, 1, 2, 3, 4];
    }
    if (Pointer_index % this.index_sum == 0 && Pointer_index > 0) {
      dot_num -= 5;
    }
    cb(Pointer_index, dot_num , click_box );
  }
  one_page(page_sum , cb) {
    let click_box = [] ;
    if ( page_sum >= 5) {
      page_sum = 5;
    }
    switch (page_sum) {
      case 0:
        click_box = [0, 1, 2, 3, 4];
        break;
      case 1:
        click_box = [0];
        break;
      case 2:
        click_box = [0, 1];
        break;
      case 3:
        click_box = [0, 1, 2];
        break;
      case 4:
        click_box = [0, 1, 2, 3];
        break;
      case 5:
        click_box = [0, 1, 2, 3, 4];
        break;
      default:
    }
    cb(click_box);
  }
  last_page(page_sum  , cb) {
    let click_box = [] ;
    let dot_num = 0;
    const index = page_sum % 5;  // 数据后面删除index个
    switch (index) {
      case 0:
        click_box = [0, 1, 2, 3, 4];
        dot_num = page_sum - 4;
        break;
      case 1:
        click_box = [0];
        dot_num = page_sum ;
        break;
      case 2:
        click_box = [0, 1];
        dot_num = page_sum - 1;
        break;
      case 3:
        click_box = [0, 1, 2];
        dot_num = page_sum - 2;
        break;
      case 4:
        click_box = [0, 1, 2, 3];
        dot_num = page_sum - 3;
        break;
      default:
    }
    cb( dot_num , click_box );
  }
  jump_to(jumptoVale, Pointer_index, dot_num, page_sum , cb) {
    let click_box = [] ;
    const $this = this;
    const num = /^\+?[1-9][0-9]*$/;
    if (num.test(jumptoVale) && jumptoVale <= page_sum) {
      Pointer_index = jumptoVale;
      if (Pointer_index > ( page_sum - page_sum % 5)) {
        const index = page_sum % 5;
        switch (index) {
          case 1:
            click_box = [0];
            break;
          case 2:
            click_box = [0, 1];
            break;
          case 3:
            click_box = [0, 1, 2];
            break;
          case 4:
            click_box = [0, 1, 2, 3];
            break;
          default:
        }
      } else {
        click_box = [0, 1, 2, 3, 4];
      }
      const j =  parseInt(jumptoVale);
      if (j % 5) {
        dot_num = j - j % 5 + 1;
      } else {
        dot_num = j - 4;
      }
      cb(jumptoVale, Pointer_index, dot_num, click_box);
    } else {
      alert('请输入合适页数');
    }
  }
  /*分页函数结束*/
}
